﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace WPF_RSSFeed
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Uri News = new Uri("http://www.ilsole24ore.com/rss/primapagina.xml");
            Uri News = new Uri("http://www.newsrimini.it/rss/homepage.xml");
        
            XElement xml = XElement.Load(News.AbsoluteUri);
            //XElement xml = XElement.Load("Dati.xml");
            var categorie = xml.Element("channel").Elements("category");

            StringBuilder elencoCategorie = new StringBuilder();
            foreach (XElement categoria in categorie)
                elencoCategorie.Append(categoria.Value + ", ");

            Console.WriteLine(elencoCategorie.ToString());

            XElement articolo = xml.Element("channel").Elements("item").First();

            Console.WriteLine("title: " + articolo.Element("title").Value);
            Console.WriteLine("description: " + articolo.Element("description").Value);

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Uri News = new Uri("http://www.newsrimini.it/rss/homepage.xml");

            XElement xml = XElement.Load(News.AbsoluteUri);

            var articoliXElement = from XElement articolo in xml.Element("channel").Elements("item")
                                   select articolo;

            var articoliAnonymous = from XElement articolo in xml.Element("channel").Elements("item")
                                    select new { Titolo = articolo.Element("title").Value, Immagine = articolo.Element("enclosure").Attribute("url").Value };

            listBox1.ItemsSource = articoliAnonymous;
        }
    }
}
